package cn.life.common.push;

import cn.life.common.entity.vo.PushVo;
import cn.life.common.push.hw.HwPush;
import cn.life.common.push.ios.IosPush;
import cn.life.common.push.jg.JgPush;
import net.sf.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description:    推送服务
 *
 * @Author 李佳明
 * @Date 2018-09-06 13:10
 */
public class PushService {

    /**
     * 推送
     */
    public void push(PushVo pushVo) throws Exception {
        switch (pushVo.getState()){
            case 2 :
                new IosPush().iosPush(pushVo);
                break;
            case 11 :
                new JgPush().androidPush(pushVo);
                break;
            case 12 :
                new HwPush().androidPush(pushVo);
                break;
        }
    }

    /**
     * @Description:    将一类的推送整理为一个pushVo实体
     *
     * @param title:推送标题
     * @param content:推送正文
     * @param state:1.安卓推送，11.极光推送，12.华为推送，2.IOS推送
     * @param pushMark:推送码
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/12/1 0001 9:21
     */
    public PushVo pushVoInit(String title,String content,int state,String pushMark){
        PushVo pushVo = new PushVo();
        pushVo.setTitle(title);
        pushVo.setContent(content);
        pushVo.setState(state);
        pushVo.setMessageType(0);
        pushVo.setCount(1);
        pushVo.setExtra(new JSONObject());

        List<String> userList = new ArrayList<String>();
        userList.add(pushMark);
        pushVo.setUserList(userList);

        return pushVo;
    }

    public PushVo pushVoInit(String title,String content,String type,List<String> pushMarkList){
        PushVo pushVo = new PushVo();
        pushVo.setTitle(title);
        pushVo.setContent(content);
        switch (type){
            case "jg" :  pushVo.setState(11); break;
            case "hw" :  pushVo.setState(12); break;
            case "ios" :  pushVo.setState(2); break;
        }
        pushVo.setMessageType(0);
        pushVo.setCount(1);
        pushVo.setExtra(new JSONObject());

        pushVo.setUserList(pushMarkList);

        return pushVo;
    }

    public void allPush(String title,String content,List<Map<String,Object>> mapList) throws Exception {
        List<String> jgMarkList = new ArrayList<>();
        List<String> hwMarkList = new ArrayList<>();
        List<String> iosMarkList = new ArrayList<>();
        for(Map<String,Object> map : mapList){
            if(map.get("jgPushMark") != null && !map.get("jgPushMark").equals("")){
                jgMarkList.add(map.get("jgPushMark").toString());
            }else if(map.get("hwPushMark") != null && !map.get("hwPushMark").equals("")){
                hwMarkList.add(map.get("hwPushMark").toString());
            }else if(map.get("iosPushMark") != null && !map.get("iosPushMark").equals("")){
                iosMarkList.add(map.get("iosPushMark").toString());
            }
        }

        if(jgMarkList.size() > 0){
            PushVo jgpushVo = pushVoInit(title,content,"jg",jgMarkList);
            push(jgpushVo);
        }

        if(hwMarkList.size() > 0){
            PushVo hwpushVo = pushVoInit(title,content,"hw",hwMarkList);
            push(hwpushVo);
        }

        if(iosMarkList.size() > 0){
            PushVo iospushVo = pushVoInit(title,content,"ios",iosMarkList);
            push(iospushVo);
        }
    }

}
